<template>
  <div>
    <el-dialog
      :title="info.issAdd?'商品编辑':'商品添加'"
      :visible.sync="info.isshow"
      @closed="closed"
      width="60%"
    >
      {{form}}
      <el-form :model="form">
        <el-form-item label="标题" label-width="120px">
          <el-input placeholder="请输入内容" v-model="form.title" autocomplete></el-input>
        </el-form-item>

        <el-form-item label="图片" label-width="120px">
          <el-upload
            class="avatar-uploader"
            action="#"
            :show-file-list="false"
            :on-change="changeFile"
          >
            <img v-if="imgUrl" :src="imgUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="form.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { errorAlert, successAlert } from "../../../utils/alert";
import { reqbanneradd, reqbanneredit ,reqbannerinfo,} from "../../../request/api";
import { mapGetters, mapActions } from "vuex";
export default {
  components: {},
  computed: {
    ...mapGetters({
      list: "banner/list",
    }),
  },
  mounted() {},
  props: ["info"],
  data() {
    return {
      imgUrl: null,
      form: {
        title: "",
        img: null,
        status: 2,
      },
    };
  },
  methods: {
    ...mapActions({
      reqList: "banner/reqList",
    }),
    // changeFile(e) {
    //   //得到文件
    //   let file = e.target.files[0];

    //   //    判断文件大小 1M=1024kb  1kb=1024b
    //   if (file.siza > 2 * 1024 * 1024) {
    //     errorAlert("文件最大2M");
    //     return;
    //   }
    //   //判断文件类型
    //   let imgArr = [".jpg", ".jpeg", ".png", ".gif"];
    //   let extname = path.extname(file.name);
    //   if (!imgArr.includes(extname)) {
    //     errorAlert("请上传正确的图片格式");
    //     return;
    //   }
    //   //根据图片新建一个地址
    //   this.imgUrl = URL.createObjectURL(file);
    //   this.form.img = file;
    // },
    changeFile(e) {
      let file = e.raw;

      //根据文件生成一个图片地址
      this.imgUrl = URL.createObjectURL(file);

      //给form.img
      this.form.img = file;
    },
    // 点击取消
    cancel() {
      this.info.isshow = false;
    },
    //清空数据
    empty() {
      this.form = {
        title: "",
        img: null,
        status: 2,
      };
      this.imgUrl = "";
    },
    // 点击添加
    add() {
      console.log(this.form);
      reqbanneradd(this.form).then((res) => {
        if (res.data.code == 200) {
          // 弹提示
          successAlert(res.data.msg);
          // 弹框消失
          this.cancel();
          //清空数据
          this.empty();
          //刷新列表
          this.reqList();
        }
      });
    },
    getOne(id) {
      reqbannerinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.id = id;
        }
        this.imgUrl = this.$pre + this.form.img;
      });
    },
    // form关闭
    closed() {
      if (!this.info.isAdd) {
        this.empty();
      }
    },
    //点击修改
    update() {
      reqbanneredit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.cancel();
          this.empty();
          this.reqList();
        }
      });
    },
  },
};
</script>
<style lang="less">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>